package controller;

import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import ui.Main;
import common.Course;
import common.User;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;


import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class MainController implements Initializable {
    private User user=Main.userClientService.getUser();
    @FXML
    private Label username;
    @FXML
    private Label number;
    @FXML
    private  ListView<Course> iTeach;
    @FXML
    private ListView<Course> iLearn;
    public static ObservableList<Course> courses1 = FXCollections.observableArrayList();
    public static ObservableList<Course> courses2 = FXCollections.observableArrayList();
    public static StringProperty nameProperty=new SimpleStringProperty();
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // 将Label的文本属性与user的name属性进行绑定
        Bindings.bindBidirectional(username.textProperty(), nameProperty);
        nameProperty.set(user.getName());
        number.setText(user.getNumber());
        iTeach.setItems(courses1);
        // 设置自定义的单元格工厂
        iTeach.setCellFactory(new Callback<ListView<Course>, ListCell<Course>>() {
            @Override
            public ListCell<Course> call(ListView<Course> param) {
                return new ListCell<Course>() {
                    @Override
                    protected void updateItem(Course course, boolean empty) {
                        super.updateItem(course, empty);
                        if (empty || course == null) {
                            setText(null);
                        } else {
                            System.out.println(course.getId()+" - "+course.getName() + " - " + course.getCode());
                            setText(String.format("%0" + 8 + "d", Integer.parseInt(course.getId()))+" - "+course.getName() + " - " + "邀请码"+course.getCode());
                        }
                    }
                };
            }
        });
        iTeach.setOnMouseClicked(event -> {
            if (event.getClickCount() == 2) {
                // 获取双击时选中的项
                Course selectedItem = iTeach.getSelectionModel().getSelectedItem();
                Stage stage=new Stage();
                FXMLLoader loader=null;
                TeacherCourseController teacherCourseController=new TeacherCourseController(selectedItem);
                Pane root=null;
                loader=new FXMLLoader(getClass().getResource("/Interface/TeacherCourse_.fxml"));
                loader.setController(teacherCourseController);
                try {
                    root=loader.load();
                } catch (IOException e) {
                    System.out.println("双击跳转我教的课程界面失败");
                    e.printStackTrace();
                }
                Scene scene=new Scene(root);
                stage.setScene(scene);
                stage.getIcons().add(new Image("/Interface/Icon.png"));
                stage.setTitle(selectedItem.getName());
                stage.setResizable(false);
                stage.show();
                stage.setOnCloseRequest(e -> {
                    // 处理窗口关闭事件
                    teacherCourseController.close();
                });
            }
        });
        iLearn.setItems(courses2);
        // 设置自定义的单元格工厂
        iLearn.setCellFactory(new Callback<ListView<Course>, ListCell<Course>>() {
            @Override
            public ListCell<Course> call(ListView<Course> param) {
                return new ListCell<Course>() {
                    @Override
                    protected void updateItem(Course course, boolean empty) {
                        super.updateItem(course, empty);
                        if (empty || course == null) {
                            setText(null);
                        } else {
                            System.out.println(course.getId()+" - "+course.getName() + " - " + course.getTeacherName());
                            setText(String.format("%0" + 8 + "d", Integer.parseInt(course.getId()))+" - "+course.getName() + " - " +course.getTeacherName()+"老师");
                        }
                    }
                };
            }
        });
        iLearn.setOnMouseClicked(event -> {
            if (event.getClickCount() == 2) {
                // 获取双击时选中的项
                Course selectedItem = iLearn.getSelectionModel().getSelectedItem();
                Stage stage=new Stage();
                FXMLLoader loader=null;
                StudentCourseController studentCourseController=new StudentCourseController(selectedItem);
                Pane root=null;
                loader=new FXMLLoader(getClass().getResource("/Interface/StudentCourse.fxml"));
                loader.setController(studentCourseController);
                try {
                    root=loader.load();
                } catch (IOException e) {
                    System.out.println("双击跳转我学的课程界面失败");
                    e.printStackTrace();
                }
                Scene scene=new Scene(root);
                stage.setScene(scene);
                stage.getIcons().add(new Image("/Interface/Icon.png"));
                stage.setTitle(selectedItem.getName());
                stage.setResizable(false);
                stage.show();
            }
        });
    }

    @FXML
    private Button newClass;
    @FXML
    public void newClass(){
        Stage stage=new Stage();
        Pane root= null;
        try {
            root= FXMLLoader.load(getClass().getResource("/Interface/NewCourse.fxml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Scene scene=new Scene(root);
        stage.setScene(scene);
        stage.getIcons().add(new Image("/Interface/Icon.png"));
        stage.setTitle("新建课程");
        stage.setResizable(false);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.show();
    }
    @FXML
    public void addClass(){
        Stage stage=new Stage();
        Pane root= null;
        try {
            root= FXMLLoader.load(getClass().getResource("/Interface/AddCourse.fxml"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Scene scene=new Scene(root);
        stage.setScene(scene);
        stage.getIcons().add(new Image("/Interface/Icon.png"));
        stage.setTitle("添加课程");
        stage.setResizable(false);
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.show();
    }
    @FXML
    public void changePwd(){
        Stage stage=new Stage();
        FXMLLoader loader=null;
        Forget changepwd=new Forget();
        Pane root=null;
        try {
            loader= new FXMLLoader(getClass().getResource("/Interface/ForgetPwd.fxml"));
            loader.setController(changepwd);
            root=loader.load();
            changepwd.getTitle().setText("修改密码");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Scene scene=new Scene(root);
        stage.setScene(scene);
        stage.getIcons().add(new Image("/Interface/Icon.png"));
        stage.setTitle("修改密码");
        stage.setResizable(false);
        stage.show();
    }
    @FXML
    public void info(){
        Stage stage=new Stage();
        Pane root= null;
        try {
            root = FXMLLoader.load(getClass().getResource("/Interface/Information.fxml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Scene scene=new Scene(root);
        stage.setScene(scene);
        stage.getIcons().add(new Image("/Interface/Icon.png"));
        stage.setTitle("超星学习通");
        stage.setResizable(false);
        stage.show();
    }
}
